import random
import time


class User(object):
    def __init__(self ,money):
        self.money = money

class Shop(object):
    def __init__(self ,name ,money ,num):
        self.name =name
        self.money = money
        self.num = num

class SuperMacket(object):
    # 初始化
    def __init__(self ,shops ,user):
        self.shops = shops
        self.shopingCart = []
        self.user = user

    #进入超市
    def enterTheSupermarket(self):
        print("进入超市，开始抽奖")
        lottery = random.randint(0 ,5)
        self.shops[lottery].money = self.shops[lottery].money - 1
        print("你抽中了"+self.shops[lottery].name+"，这个商品价格-1")
        self.printShops()
        while True:
            chiose = input("你想要继续购物吗，输入1继续，输入2退货，其他退出")
            if  chiose == '1':
                self.printShops()
            elif chiose == '2':
                self.returnShop()
            else:
                self.printShopCart()
                break

    # 打印商品
    def printShops(self):
        id = 1
        for shop in self.shops:
            print('商品id：', end='')
            print(id ,end='')
            print("--商品名：", end='')
            print(shop.name, end='')
            print("--价格：", end='')
            print(shop.money)
            id = id + 1
        chiose = int(input("请输入你想要的商品的id："))
        if id < chiose:
            print("你输入的有误，请再次输入")
            self.printShops()
        else:
            self.addShop(chiose - 1)

    # 添加购物车
    def addShop(self ,index):
        if self.user.money >= self.shops[index].money:
            if self.shops[index] in self.shopingCart:
                cartAdd = self.shopingCart.index(self.shops[index])
                self.shopingCart[cartAdd].num = self.shopingCart[cartAdd].num + 1
            else :
                self.shopingCart.append(self.shops[index])
            self.user.money = self.user.money - self.shops[index].money
            print("添加成功，您的余额还剩下：" ,end='')
            print(self.user.money)
        else:
            print("穷鬼，钱不够！请到其他超市购买!")


    # 打印小票
    def printShopCart(self):
        cartMoney = 0
        for shop in self.shopingCart:
            print("商品名：" ,end='')
            print(shop.name ,end='')
            print("--商品数量：", end='')
            print(shop.num, end='')
            print("--价格：" ,end='')
            print(shop.money*shop.num)
            cartMoney = cartMoney +shop.money*shop.num
        print("您的卡余额为：", end='')
        print(self.user.money)
        print("您的购物时间为：", end='')
        print(time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime()))
        print("您的购物商品的总价值为：", end='')
        print(cartMoney)

    def returnShop(self):
        self.printShopCart()
        shopName = input('请输入你想要退货的商品')
        index = 0
        for shop in self.shopingCart:
            if shop.name == shopName:
                self.shopingCart.remove(shop)
                index = 1
                break
        if index == 0:
            print("您的输入有误，请重新输入")
            self.returnShop()
        else:
            print("退货成功")


money = int(input("请输入你想要带的金钱数（整数）"))
superMacket = SuperMacket(
    [Shop('盼盼薯片' ,5 ,1) ,Shop('汪汪雪饼' ,2 ,1) ,Shop('可口可乐' ,3 ,1)
        ,Shop('芬达' ,3 ,1) ,Shop('康师傅冰红茶' ,5 ,1) ,Shop('牛肉粒' ,15 ,1)]
    ,User(money))
superMacket.enterTheSupermarket()

